package com.xdk.templatescala.dao;

import com.xdk.templatescala.common.constants.ScaleAnswerContant;
import com.xdk.templatescala.common.enums.ScaleStatusEnum;
import com.xdk.templatescala.model.ScaleAnswerEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class ScaleAnswerDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 获取所有的量表模板
     *
     * @return
     */
    public List<ScaleAnswerEntry> findAll() {
        Query query = Query.query(Criteria.where(ScaleAnswerContant.STATUS).is(ScaleStatusEnum.SCALE_STATUS_1.getCode()));
        return mongoTemplate.find(query,ScaleAnswerEntry.class);
    }

    /**
     * 找到所有id
     *
     * @param scaleAnswerIds 规模回答id
     * @return {@link List}<{@link ScaleAnswerEntry}>
     */
    public List<ScaleAnswerEntry> findAllById(List<String> scaleAnswerIds){
        Query query = Query.query(Criteria.where(ScaleAnswerContant.STATUS).is(ScaleStatusEnum.SCALE_STATUS_1.getCode()).and(ScaleAnswerContant.ANSWER_ID).in(scaleAnswerIds));
        return mongoTemplate.find(query,ScaleAnswerEntry.class);
    }

    /**
     * 根据scaleAnswerId获取量表模板
     *
     * @return
     */
    public ScaleAnswerEntry findOne(String scaleAnswerId) {
        Query query = Query.query(Criteria.where(ScaleAnswerContant.ANSWER_ID).is(scaleAnswerId));
        return mongoTemplate.findOne(query,ScaleAnswerEntry.class);
    }


    /**
     * 添加量表模板
     *
     * @param scaleAnswerEntry
     */
    public void addScaleAnswerEntry(ScaleAnswerEntry scaleAnswerEntry) {
        mongoTemplate.save(scaleAnswerEntry);
    }

    /**
     * 根据scaleAnswerIds删除量表模板
     *
     * @return
     */
    public Long delScaleAnswers(String scaleAnswerIds) {
        String[] ids = scaleAnswerIds.split(",");
        Query query = Query.query(Criteria.where(ScaleAnswerContant.ANSWER_ID).in(Arrays.asList(ids)));
        return mongoTemplate.remove(query, ScaleAnswerEntry.class).getDeletedCount();
    }
}
